package com.company.oa.document.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.document.dto.DocumentDTO;
import com.company.oa.document.entity.Document;
import com.company.oa.document.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Map;

/**
 * 公文控制器
 */
@RestController
@RequestMapping("/documents")
public class DocumentController {

    @Autowired
    private DocumentService documentService;

    /**
     * 分页查询公文列表
     */
    @GetMapping("/page")
    public Result<IPage<Document>> getDocumentPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            Document document) {
        IPage<Document> documentPage = documentService.getDocumentPage(page, size, document);
        return Result.success(documentPage);
    }

    /**
     * 查询待我审批的公文列表
     */
    @GetMapping("/pending")
    public Result<IPage<Document>> getPendingApprovalPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam Long userId) {
        IPage<Document> documentPage = documentService.getPendingApprovalPage(page, size, userId);
        return Result.success(documentPage);
    }

    /**
     * 查询我已审批的公文列表
     */
    @GetMapping("/approved")
    public Result<IPage<Document>> getApprovedPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam Long userId) {
        IPage<Document> documentPage = documentService.getApprovedPage(page, size, userId);
        return Result.success(documentPage);
    }

    /**
     * 查询我创建的公文列表
     */
    @GetMapping("/my")
    public Result<IPage<Document>> getMyDocumentPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam Long userId) {
        IPage<Document> documentPage = documentService.getMyDocumentPage(page, size, userId);
        return Result.success(documentPage);
    }

    /**
     * 查询抄送给我的公文列表
     */
    @GetMapping("/cc")
    public Result<IPage<Document>> getCcToMePage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam Long userId) {
        IPage<Document> documentPage = documentService.getCcToMePage(page, size, userId);
        return Result.success(documentPage);
    }

    /**
     * 获取公文详情
     */
    @GetMapping("/{id}")
    public Result<Document> getDocumentDetail(@PathVariable Long id) {
        Document document = documentService.getDocumentDetail(id);
        if (document == null) {
            return Result.failed("公文不存在");
        }
        return Result.success(document);
    }

    /**
     * 创建公文
     */
    @PostMapping
    public Result<Boolean> createDocument(@RequestBody @Valid DocumentDTO documentDTO) {
        boolean result = documentService.createDocument(documentDTO);
        return Result.success(result);
    }

    /**
     * 更新公文
     */
    @PutMapping
    public Result<Boolean> updateDocument(@RequestBody @Valid DocumentDTO documentDTO) {
        boolean result = documentService.updateDocument(documentDTO);
        return Result.success(result);
    }

    /**
     * 删除公文
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteDocument(@PathVariable Long id) {
        boolean result = documentService.deleteDocument(id);
        return Result.success(result);
    }

    /**
     * 提交公文审批
     */
    @PostMapping("/{id}/submit")
    public Result<Boolean> submitDocument(@PathVariable Long id) {
        boolean result = documentService.submitDocument(id);
        return Result.success(result);
    }

    /**
     * 归档公文
     */
    @PostMapping("/{id}/archive")
    public Result<Boolean> archiveDocument(@PathVariable Long id) {
        boolean result = documentService.archiveDocument(id);
        return Result.success(result);
    }

    /**
     * 上传附件
     */
    @PostMapping("/attachment")
    public Result<Long> uploadAttachment(
            @RequestParam("file") MultipartFile file,
            @RequestParam(required = false) Long documentId,
            @RequestParam Long uploaderId,
            @RequestParam String uploaderName) {
        Long attachmentId = documentService.uploadAttachment(file, documentId, uploaderId, uploaderName);
        return Result.success(attachmentId);
    }

    /**
     * 删除附件
     */
    @DeleteMapping("/attachment/{id}")
    public Result<Boolean> deleteAttachment(@PathVariable Long id) {
        boolean result = documentService.deleteAttachment(id);
        return Result.success(result);
    }

    /**
     * 获取公文统计数据
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getDocumentStatistics(@RequestParam Long userId) {
        Map<String, Object> statistics = documentService.getDocumentStatistics(userId);
        return Result.success(statistics);
    }
} 